package zhihuilan.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.Collections;
import java.util.List;

/**
 * 分页查询结果基类
 *
 * @author chenk
 */
@Data
public class WxMiniPageDto<T> {

    protected List<T> data;
    protected long total;
    protected long size;
    protected long current;

    protected long pageTotal;

    @JsonProperty("last_page")
    protected long lastPage;

    public WxMiniPageDto(){
        this.data = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        this.lastPage=1L;
    }

    public WxMiniPageDto(long current, long size) {
        this.data = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        this.lastPage=1L;
        if (current >= 1L) {
            this.current = current;
        }
        if (size >= 1L) {
            this.size = size;
        }
    }

    public List<T> getData() {
        return this.data;
    }

    public WxMiniPageDto<T> setData(List<T> data) {
        this.data = data;
        return this;
    }

    public long getTotal() {
        return this.total;
    }

    public WxMiniPageDto<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    public long getSize() {
        return this.size;
    }

    public WxMiniPageDto<T> setSize(long size) {
        this.size = size;
        return this;
    }

    public long getCurrent() {
        return this.current;
    }

    public WxMiniPageDto<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    public long getPageTotal(){
        long pageToatl = (this.total / this.size)+1l;
        return pageToatl <= 0l ? 1l : pageToatl;
    }

    public long getLastPage() {
        long pages=(long) Math.ceil((double) this.total/this.size);
        this.lastPage=pages;
        return lastPage;
    }
}
